package com.syh.jn.scm.infrastructure.repository.impl.operation.financial;

import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.infrastructure.repository.AbstractAggregateRepository;
import com.syh.jn.scm.domain.operation.entity.financial.FinancialContact;
import com.syh.jn.scm.domain.operation.repository.financial.FinancialContactRepository;
import com.syh.jn.scm.dto.operation.financial.FinancialContactVo;
import com.syh.jn.scm.infrastructure.repository.model.base.SupplierModel;
import com.syh.jn.scm.infrastructure.repository.model.base.financial.FinanceModel;
import com.syh.jn.scm.infrastructure.repository.model.base.financial.SupplierFinanceModel;
import com.syh.jn.scm.infrastructure.repository.utils.SQLAliasGenerateUtil;
import com.syh.jn.scm.infrastructure.wrapper.BaseResolve;
import com.syh.jn.scm.infrastructure.wrapper.ResolveToSQL;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Repository;

import java.util.HashMap;

@Repository
public class FinancialContactRepositoryImpl extends AbstractAggregateRepository<FinancialContact, SupplierFinanceModel,Long> implements FinancialContactRepository {
  /**
   * 暂停合作
   *
   * @param financialContact
   * @return
   */
  @Override
  public Boolean disabled(FinancialContact financialContact) {
    ExtAssert.notNull(financialContact,"对象为空");
    super.update(financialContact);
    return Boolean.TRUE;
  }

  /**
   * 开启合作
   *
   * @param financialContact
   * @return
   */
  @Override
  public Boolean enabled(FinancialContact financialContact) {
    ExtAssert.notNull(financialContact,"对象为空");
    super.update(financialContact);
    return Boolean.TRUE;
  }

  @Override
  protected SQL getViewSql() {
    //model数组
    Class[] classes = {
            SupplierFinanceModel.class,
            FinanceModel.class,
            SupplierModel.class
    };
    //别名数组
    String[] aliases = {
            "supplierFin",
            "fin",
            "supplier"
    };
    return new SQL() {{
      SELECT(SQLAliasGenerateUtil.aliasGenerate(classes,aliases,null),
              "supplier.NAME AS supplierName","fin.Name AS financialName");
      FROM(getModelTableName(SupplierFinanceModel.class)+" supplierFin ");
      INNER_JOIN(getModelTableName(FinanceModel.class)+" fin on fin.ID = supplierFin.BUSINESS_ENTITY_ID");
      INNER_JOIN(getModelTableName(SupplierModel.class)+" supplier on supplier.ID = supplierFin.SUPPLIER_ID");
    }};
  }

  @Override
  protected Class getViewObject() {
    return FinancialContactVo.class;
  }

  @Override
  protected BaseResolve getViewWrapperResolve(Wrapper wrapper) {
    HashMap<String,String> weMap = new HashMap<>();
    weMap.put("id", "supplierFin.ID");
    weMap.put("name", "supplier.NAME");
    weMap.put("businessEntityId", "supplierFin.BUSINESS_ENTITY_ID");
    ResolveToSQL parse = new ResolveToSQL(wrapper, (propertyName) -> {
      return weMap.get(propertyName);
    });
    return parse;
  }
}
